package com.egoo.ticket.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.dao.FormTemplateDao;
import com.egoo.ticket.server.dao.WorkOrderFieldReflectDao;
import com.egoo.ticket.server.pojo.dto.WorkOrderFieldReflectDto;
import com.egoo.ticket.server.pojo.entity.FormTemplate;
import com.egoo.ticket.server.pojo.entity.WorkOrderFieldReflect;
import com.egoo.ticket.server.service.WorkOrderFieldReflectService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: WorkOrderFieldReflectServiceImp
 * @Description:
 * @Author: Donny_Dong
 * @CreateDate: 2019/12/25
 * @Version: 1.0
 **/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class WorkOrderFieldReflectServiceImpl implements WorkOrderFieldReflectService {


	@Autowired
	private WorkOrderFieldReflectDao workOrderFieldReflectDao;
	@Autowired
	private FormTemplateDao formTemplateDao;

	/**
	 * @Author Donny_Dong
	 * @Description // 创建外部工单字段映射模板
	 * @Date 15:21 2019/12/25
	 * @Param [workOrderFieldReflectDto]
	 * @return com.egoo.ews.service.utils.DBResponse
	 **/
	@Override
	public DbResponse creat(WorkOrderFieldReflectDto workOrderFieldReflectDto) {

		String tenantId = workOrderFieldReflectDto.getTenantId();
		String workOrderTypeId = workOrderFieldReflectDto.getWorkOrderTypeId();
		JSONArray workOrderFieldReflectArray = workOrderFieldReflectDto.getWorkOrderFieldReflect();
		List<WorkOrderFieldReflect> workOrderFieldReflects = JSONArray.parseArray(workOrderFieldReflectArray.toJSONString(), WorkOrderFieldReflect.class);
		for (WorkOrderFieldReflect workOrderFieldReflect : workOrderFieldReflects) {
			workOrderFieldReflect.setWorkOrderTypeId(workOrderTypeId);
			workOrderFieldReflect.setWorkOrderTypeNodeId(workOrderFieldReflectDto.getWorkOrderTypeNodeId());
			if (StringUtils.isEmpty(workOrderFieldReflect.getTenantId())) {
				workOrderFieldReflect.setTenantId(tenantId);
			}
		}
		workOrderFieldReflectDao.create(workOrderFieldReflects);
		return new DbResponse(ResponseConstant.RESULT_SUCCESS,"字段映射模板创建成功");
	}


	/**
	 * @Author Donny_Dong
	 * @Description //TODO 获取映射字段含义
	 * @Date 11:56 2019/12/27
	 * @Param [id]
	 * @return com.egoo.ews.service.utils.DBResponse
	 **/

	@Override
	public DbResponse getWorkOrderFieldMeaning(Integer id) {
		FormTemplate formTemplate = formTemplateDao.selectById(id);
		if (null == formTemplate){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"该模板不存在");
		}
		JSONObject formTemplateContent = formTemplate.getContent();
		List<WorkOrderFieldReflect> workOrderFieldReflects = new ArrayList<>();
		workOrderFieldReflects = translateField(workOrderFieldReflects,
				formTemplateContent);
		workOrderFieldReflects = workOrderFieldReflects.stream().map(workOrderFieldReflect -> workOrderFieldReflect.setFormTemplateId(id.toString()).setTenantId(formTemplate.getTenantId())).collect(Collectors.toList());
		Object object = assembleWorkOrderFieldReflect(workOrderFieldReflects);
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,"数据获取成功",object);
	}

	/**
	 * @Author Donny_Dong
	 * @Description //TODO 组装工单映射字段
	 * @Date 14:00 2019/12/27
	 * @Param [workOrderFieldReflects]
	 * @return void
	 **/
	private Object assembleWorkOrderFieldReflect(List<WorkOrderFieldReflect> workOrderFieldReflects) {
//		workOrderFieldReflects.add(new WorkOrderFieldReflect("title", ConstantCode.WorkOrderBaseFieldMeanig.TITLE,"",""));
//		workOrderFieldReflects.add(new WorkOrderFieldReflect("externalWorkOrderId",ConstantCode.WorkOrderBaseFieldMeanig.EXTERNAL_WORK_ORDERID,"",""));
//		workOrderFieldReflects.add(new WorkOrderFieldReflect("source",ConstantCode.WorkOrderBaseFieldMeanig.SOURCE,"",""));
//		workOrderFieldReflects.add(new WorkOrderFieldReflect("priority",ConstantCode.WorkOrderBaseFieldMeanig.PRIORITY,"",""));
//		workOrderFieldReflects.add(new WorkOrderFieldReflect("clientName",ConstantCode.WorkOrderBaseFieldMeanig.CLIENT_NAME,"",""));
//		workOrderFieldReflects.add(new WorkOrderFieldReflect("phone",ConstantCode.WorkOrderBaseFieldMeanig.PHONE,"",""));
//		workOrderFieldReflects.add(new WorkOrderFieldReflect("problemDesc",ConstantCode.WorkOrderBaseFieldMeanig.PROBLEMDESC,"",""));
//		workOrderFieldReflects.add(new WorkOrderFieldReflect("customField",ConstantCode.WorkOrderBaseFieldMeanig.CUSTOM_FIELD,"",""));
//		workOrderFieldReflects.add(new WorkOrderFieldReflect("combineWorkOrderIds",ConstantCode.WorkOrderBaseFieldMeanig.MERGE_WORK_ORDER,"",""));
		Object object = JSON.toJSON(workOrderFieldReflects);
		return object;
	}

	/**
	 * @Author Donny_Dong
	 * @Description //TODO 翻译模板字段
	 * @Date 13:14 2019/12/27
	 * @Param [formTemplateContent]
	 * @return void
	 **/
	@Override
	public List<WorkOrderFieldReflect> translateField(List workOrderFieldReflects, JSONObject formTemplateContent) {
		JSONArray jsonArray = formTemplateContent.getJSONArray(ConstantCode.FormTemplateType.LIST);
		for (int i = 0; i <jsonArray.size() ; i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			if (ConstantCode.FormTemplateType.GRID.equals(jsonObject.getString(ConstantCode.FormTemplateType.TYPE))){
				JSONArray columnsJsonArray = jsonObject.getJSONArray(ConstantCode.FormTemplateType.COLUMNS);
				for (int j = 0; j <columnsJsonArray.size() ; j++) {
					JSONObject jsonObjectColumns = columnsJsonArray.getJSONObject(j);
					JSONArray jsonObjectColumnsArray = jsonObjectColumns.getJSONArray(ConstantCode.FormTemplateType.LIST);
					for (int k = 0; k < jsonObjectColumnsArray.size(); k++) {
						String model = jsonObjectColumnsArray.getJSONObject(k).getString(ConstantCode.FormTemplateType.MODEL);
						String name = jsonObjectColumnsArray.getJSONObject(k).getString(ConstantCode.FormTemplateType.NAME);
						workOrderFieldReflects.add(new WorkOrderFieldReflect(model,name,"",""));
					}

				}
			}else {

				String model = jsonObject.getString(ConstantCode.FormTemplateType.MODEL);
				String name = jsonObject.getString(ConstantCode.FormTemplateType.NAME);
				workOrderFieldReflects.add(new WorkOrderFieldReflect(model,name,"",""));
			}
		}
		return workOrderFieldReflects;
	}
}
